package com.ruoyi.wechat.service.impl;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.wechat.domain.Details;
import com.ruoyi.wechat.service.IDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wechat.mapper.AppointmentMapper;
import com.ruoyi.wechat.domain.Appointment;
import com.ruoyi.wechat.service.IAppointmentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 营地预约Service业务层处理
 * 
 * @author lsp
 * @date 2024-07-30
 */
@Service
public class AppointmentServiceImpl implements IAppointmentService 
{
    @Autowired
    private AppointmentMapper appointmentMapper;

    @Autowired
    private IDetailsService detailsService;


    @Override
    public List<Integer> listInfoNumber() {
        List<Integer> maps = appointmentMapper.listInfoNumber();

        Collections.reverse(maps);

        return maps;
    }

    @Override
    public List<BigDecimal> getSalesMoney() {

        return appointmentMapper.getSalesMoney();
    }

    /**
     * 查询营地预约
     * 
     * @param appointId 营地预约主键
     * @return 营地预约
     */
    @Override
    public Appointment selectAppointmentByAppointId(String appointId)
    {
        return appointmentMapper.selectAppointmentByAppointId(appointId);
    }

    /**
     * 查询营地预约列表
     * 
     * @param appointment 营地预约
     * @return 营地预约
     */
    @Override
    public List<Appointment> selectAppointmentList(Appointment appointment)
    {
        return appointmentMapper.selectAppointmentList(appointment);
    }

    /**
     * 新增营地预约
     * 
     * @param appointment 营地预约
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAppointment(Appointment appointment)
    {
        appointment.setAppointId(IdUtils.randomUUID());
        for (int i = 0; i < appointment.getDetailsList().size(); i++) {
            appointment.getDetailsList().get(i).setAppointId(appointment.getAppointId());
            appointment.getDetailsList().get(i).setDetailId(IdUtils.randomUUID());
            detailsService.insertDetails(appointment.getDetailsList().get(i));
        }
        return appointmentMapper.insertAppointment(appointment);
    }

    /**
     * 修改营地预约
     * 
     * @param appointment 营地预约
     * @return 结果
     */
    @Override
    public int updateAppointment(Appointment appointment)
    {

        // 一对多关系维护第一种，先删除旧数据，然后全部添加新数据
//        detailsService.deleteDetailsByAppointId(appointment.getAppointId());
//        for (int i = 0; i < appointment.getDetailsList().size(); i++) {
//            appointment.getDetailsList().get(i).setAppointId(appointment.getAppointId());
//            appointment.getDetailsList().get(i).setDetailId(IdUtils.randomUUID());
//            detailsService.insertDetails(appointment.getDetailsList().get(i));
//        }

        // 一对多关系维护第二种 通过比较新旧数据，删除多余的数据，更新或新增数据
        List<Details> detailsList = appointment.getDetailsList();
        List<Details> detailsNowList = detailsService.selectDetailsByAppointId(appointment.getAppointId());
        // 删除多余的数据
        for (Details nowDetails : detailsNowList)
        {
            int flag = 0;
            for (Details details : detailsList)
            {
                if (nowDetails.getDetailId().equals(details.getDetailId()))
                {
                    flag = 1;
                    break;
                }
            }
            if (flag == 0) {
                detailsService.deleteDetailsByDetailId(nowDetails.getDetailId());
            }
        }
        // 更新或新增数据
        for (Details details : detailsList)
        {
            if (StringUtils.isEmpty(details.getDetailId()))
            {
                details.setDetailId(IdUtils.randomUUID());
                details.setAppointId(appointment.getAppointId());
                detailsService.insertDetails(details);
            }
            else
            {
                detailsService.updateDetails(details);
            }
        }


        return appointmentMapper.updateAppointment(appointment);
    }

    /**
     * 批量删除营地预约
     * 
     * @param appointIds 需要删除的营地预约主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentByAppointIds(String[] appointIds)
    {
        return appointmentMapper.deleteAppointmentByAppointIds(appointIds);
    }

    /**
     * 删除营地预约信息
     * 
     * @param appointId 营地预约主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentByAppointId(String appointId)
    {
        return appointmentMapper.deleteAppointmentByAppointId(appointId);
    }
}
